package com.sgb.kjwl.viewmodel.demand;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.DemandHallListEntity;
import com.sgb.kjwl.model.entity.EnquiryTypeEntity;
import com.sgb.kjwl.model.entity.IndustryHomeListEntity;
import com.sgb.kjwl.model.entity.manage.GoodsInquiryEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.demandHall.CustomerInquiryFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.entity.MenuEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author qianghe
 * 说明：商品询价
 * 时间：2021.04.10
 */
public class CustomerInquiryModel extends BaseViewModel {
    private CustomerInquiryFragment fragment;
    private HomeRepertory repertory;
    private final int pageLimit = 10;

    public CustomerInquiryModel(CustomerInquiryFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /***
     * 获取材料类型数据
     */
    public void getDemandHallList() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getDemandHall("").enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null) {
                        return;
                    }
                    fragment.getCustomerInquiryTabList(response.body().getData());
                } else {
                    MToast.showToast(fragment.getContext(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
//                Log.e("tag数据失败","数据异常："+t.getMessage()+t.toString() );
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getContext(), t.getMessage());
            }
        });
    }

    /***
     *  反向查询商品需求分类
     */
    public void getReverseLookupTreeNodeByEn(String companyNo) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getReverseLookupTreeNodeByEn(companyNo).enqueue(new Callback<BaseEntity<List<GoodsInquiryEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInquiryEntity>>> call, Response<BaseEntity<List<GoodsInquiryEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<GoodsInquiryEntity> goodsInquiryEntities = response.body().getData();
                    if (goodsInquiryEntities == null) {
                        return;
                    }
                    List<DictEntity3> dictEntity3List = new ArrayList<>();
                    for (GoodsInquiryEntity inquiryEntity : goodsInquiryEntities) {
                        DictEntity3 dictEntity3 = new DictEntity3();
                        dictEntity3.setId(String.valueOf(inquiryEntity.getId()));
                        dictEntity3.setChecked(false);
                        dictEntity3.setName(inquiryEntity.getName());
                        dictEntity3List.add(dictEntity3);
                    }

                    fragment.getCustomerInquiryTabList(dictEntity3List);
                } else {
                    MToast.showToast(fragment.getContext(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInquiryEntity>>> call, Throwable t) {
//                Log.e("tag数据失败","数据异常："+t.getMessage()+t.toString() );
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getContext(), t.getMessage());
            }
        });
    }

    /***
     *  获取状态类型
     */
    public void getEnquiryType() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getEnquiryType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null) {
                        return;
                    }
                    fragment.getEnquirtTypeList(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    /****
     * 获取排序类型
     */
    public void getSortType() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getSortType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null) {
                        return;
                    }
                    fragment.getSortTypeList(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    /****
     * 获取数据列表信息
     * @param userid
     * @param sortid
     * @param enquiryid
     * @param fcatName
     * @param searchname
     * @param page
     */
    public void getDemandHallList(String userid, String sortid, String enquiryid, String fcatName, String searchname, String companyNo, int page) {
        Map map = new HashMap(7);
        if (!TextUtils.isEmpty(userid)) {
            map.put("authMethod", userid);
        }
        map.put("limit", pageLimit);
        map.put("page", page);
        map.put("sortType", sortid);
        if (!TextUtils.isEmpty(enquiryid)) {
            map.put("status", enquiryid);
        }
        map.put("fcatName", fcatName);
        map.put("tcatName", searchname);
        map.put("companyNo", companyNo);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getDemandHallList(map).enqueue(new Callback<BaseEntity<ListResponse<List<DemandHallListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Response<BaseEntity<ListResponse<List<DemandHallListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null) {
                        return;
                    }
                    fragment.setDemandHallList(response.body().getData().getList());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    /****
     * 需求列表详情界面
     * @param no
     */
    public void showUrl(String no,String shareTitle) {
        APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("enquiryDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!TextUtils.isEmpty(response.body().getData().getUrl())) {
                        String url = response.body().getData().getUrl() + "?no=" + no;
                        WebViewX5HostActivity.start(fragment.getActivity(), url,"","customerinquiry",shareTitle);
                        //存储商品跳转h5标记
                        SharedPreferenceUtil.getInstance().saveString("EVENT_SHOPPING_TYPE", "event_shopping_list");
                    } else {
                        WebViewX5HostActivity.start(fragment.getActivity(), "");
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {

            }
        });
    }


    public void getIndustryListdata(){

        NewAPIManager.getInstance().getGoodsAPI().getIndustryList().enqueue(new Callback<BaseEntity<IndustryHomeListEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<IndustryHomeListEntity>> call, Response<BaseEntity<IndustryHomeListEntity>> response) {
                if (response.body() == null) {
                    getDemandHallList();
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showTabIndustryList(response.body().getData());
                } else {
                    getDemandHallList();
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<IndustryHomeListEntity>> call, Throwable t) {
                getDemandHallList();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });

    }


    public void getMenuModel(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        Map map = new HashMap<String,Object>();
        if (map == null) {
            map = new HashMap(2);
        }
        map.put("industryId", id);
//        map.put("areaCode", fragment.getAreaCode());
        APIManager.getInstance().getMaterialAPI().getItemCatList(map).enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(@NotNull Call<BaseEntity<List<MenuEntity>>> call, @NotNull Response<BaseEntity<List<MenuEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<DictEntity3> listdata = new ArrayList<>();
                    for (int i = 0; i < response.body().getData().size(); i++) {
                        DictEntity3 data = new DictEntity3();
                        data.setId(response.body().getData().get(i).getId());
                        data.setName(response.body().getData().get(i).getName());

                        listdata.add(data);
                    }

                    fragment.getCustomerInquiryTabList(listdata);
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(@NotNull Call<BaseEntity<List<MenuEntity>>> call, @NotNull Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }
}
